package Window;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.sql.SQLException;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.*;

import Window.DetailWidget.MenuButton;

@SuppressWarnings("serial")
public class OnlinebattlePanel extends JPanel {

	private JLabel usernameJLabel;
	private JLabel passwordJLabel;
	private JTextField userID;
	private JPasswordField password;
	private MenuButton buttonlogin;
	public  static Clip clip;
    JDBConnection jdbConnection = new JDBConnection();


	public OnlinebattlePanel() {
		usernameJLabel = new JLabel("\u8D26\u53F7");
		usernameJLabel.setFont(new Font("华文新魏", Font.PLAIN, 58));
		usernameJLabel.setBounds(483, 189, 116, 100);
		this.add(usernameJLabel);

		passwordJLabel = new JLabel("\u5BC6\u7801");
		passwordJLabel.setFont(new Font("华文新魏", Font.PLAIN, 58));
		passwordJLabel.setBounds(483, 353, 116, 90);
		add(passwordJLabel);

		userID = new JTextField();
		userID.setFont(new Font("微软雅黑", Font.PLAIN, 36));
		userID.setBounds(677, 215, 349, 63);
		this.add(userID);

		password = new JPasswordField();
		password.setFont(new Font("微软雅黑", Font.PLAIN, 36));
		password.setBounds(677, 374, 349, 63);
		this.add(password);

		buttonlogin = new MenuButton();
		buttonlogin.setBounds(483, 535, 162, 81);
		buttonlogin.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/admin.png")));
		this.add(buttonlogin);

		buttonlogin.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				buttonlogin.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/admin2.png")));

			}

			@Override
			public void mousePressed(MouseEvent e) {
				String nameString  = userID.getText();
				String passwordString = new String(password.getPassword());
				if("".equals(nameString.trim()))
					JOptionPane.showMessageDialog(null, "账号不能为空！", "用户名", JOptionPane.ERROR_MESSAGE);
				else if("".equals(passwordString.trim()))
				{
					JOptionPane.showMessageDialog(null, "密码不能为空！", "密码为空", JOptionPane.ERROR_MESSAGE);
				}
				else {
					try {
						if(jdbConnection.check(nameString, passwordString)) {
							JOptionPane.showMessageDialog(null, "登录成功！", "登录成功", JOptionPane.INFORMATION_MESSAGE);
							try {
								File audioFile = new File("src/Window/musics/背景音乐.wav");
		//						System.out.println(audioFile.getAbsolutePath());
								AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
								clip = AudioSystem.getClip();
								clip.open(audioStream);
								clip.loop(Clip.LOOP_CONTINUOUSLY); // 循环播放

							} catch (Exception ex) {
								ex.printStackTrace();
							}

							// 启动游戏窗口
							GoBangFrame win = new GoBangFrame();
							win.start();

							// 关闭当前窗口
							Window window = SwingUtilities.getWindowAncestor((Component) e.getSource());
							window.dispose();
						}

						else{
							JOptionPane.showMessageDialog(null, "账号或密码错误！", "账号或密码错误", JOptionPane.ERROR_MESSAGE);
						}
					} catch (SQLException throwables) {
						throwables.printStackTrace();
					}
				}
				buttonlogin.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/admin.png")));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				buttonlogin.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/admin.png")));
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				buttonlogin.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/admin2.png")));
			}

			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});

		buttonlogin.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(()->{
				}).start();
			}
		});

		MenuButton buttonregister = new MenuButton();
		buttonregister.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register.png")));
		buttonregister.setBounds(864, 535, 162, 81);
		this.add(buttonregister);

		buttonregister.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				buttonregister
						.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register2.png")));
				MainWindow.cardLayout.show(MainWindow.mainPanel, "注册界面");

			}

			@Override
			public void mousePressed(MouseEvent e) {
				buttonregister
						.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register.png")));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				buttonregister
						.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register.png")));
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				buttonregister
						.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/register2.png")));
			}

			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		backgroundPanel();// 搭建背景和返回主页按钮
	}
	public void backgroundPanel() {
		// 搭建界面
		setSize(MainWindow.WIDTH, MainWindow.HEIGHT);
		setLayout(null);

		// 设置背景图片
		JLabel bg_image = new JLabel();
		bg_image.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/background.png")));
		bg_image.setSize(MainWindow.WIDTH, MainWindow.HEIGHT);
		new ImageIcon("background.png");

		// 返回按钮
		MenuButton button_backButton = new MenuButton();
		button_backButton.setBounds(0, MainWindow.HEIGHT - 150, 100, 100);
		button_backButton.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back.png")));
		this.add(button_backButton);

		button_backButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				button_backButton.setIcon(
						new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back_enter.png")));
				MainWindow.cardLayout.show(MainWindow.mainPanel, "主界面");

			}

			@Override
			public void mousePressed(MouseEvent e) {
				button_backButton.setIcon(
						new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back_pressed.png")));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				button_backButton
						.setIcon(new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back.png")));

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				button_backButton.setIcon(
						new ImageIcon(OnlinebattlePanel.class.getResource("/Window/image/button_back_enter.png")));

			}

			@Override
			public void mouseClicked(MouseEvent e) {

			}
		});
		this.add(bg_image);// 将背景添加到容器中
	}

}